package com.yang.dao;

import com.yang.entity.Productcar;

import java.sql.*;
import java.util.*;

public class ProductcarDao {
    //查询全部
    public List<Productcar> queryProductcar() {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("select * from Productcar");
            rs = ps.executeQuery();
            List<Productcar> list = new ArrayList<>();
            while (rs.next()) {
                Productcar obj = new Productcar();
                obj.setId(rs.getInt("id"));
                obj.setName(rs.getString("name"));
                obj.setUrl(rs.getString("url"));
                obj.setPrice(rs.getObject("price"));
                obj.setNum(rs.getInt("num"));
                obj.setDetail(rs.getString("detail"));
                obj.setCategoryid(rs.getInt("categoryid"));
                obj.setUid(rs.getInt("uid"));
                list.add(obj);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(rs, ps, conn);
        }
        return null;
    }

    //单个查询
    public Productcar queryProductcarById(Integer id) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("select * from Productcar where id=?");
            ps.setInt(1, id);
            rs = ps.executeQuery();
            if (rs.next()) {
                Productcar obj = new Productcar();
                obj.setId(rs.getInt("id"));
                obj.setName(rs.getString("name"));
                obj.setUrl(rs.getString("url"));
                obj.setPrice(rs.getObject("price"));
                obj.setNum(rs.getInt("num"));
                obj.setDetail(rs.getString("detail"));
                obj.setCategoryid(rs.getInt("categoryid"));
                obj.setUid(rs.getInt("uid"));
                return obj;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(rs, ps, conn);
        }
        return null;
    }

    //添加
    public int saveProductcar(Productcar obj) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("insert into productcar(name, url, price, num, detail, categoryId) values (?, ?, ?, ?, ?, ?)");
            ps.setString(1, obj.getName());
            ps.setString(2, obj.getUrl());
            ps.setObject(3, obj.getPrice());
            ps.setInt(4, obj.getNum());
            ps.setString(5, obj.getDetail());
            ps.setInt(6, obj.getCategoryid());
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(null, ps, conn);
        }
        return 0;
    }

    //删除
    public int deleteProductcarById(Integer id) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("delete from Productcar where id=?");
            ps.setInt(1, id);
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(null, ps, conn);
        }
        return 0;
    }

    //修改
    public int updateProductcarById(Productcar obj) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("update productcar set name=?, url=?, price=?, num=?, detail=?, categoryId=? where id=?");
            ps.setString(1, obj.getName());
            ps.setString(2, obj.getUrl());
            ps.setObject(3, obj.getPrice());
            ps.setInt(4, obj.getNum());
            ps.setString(5, obj.getDetail());
            ps.setInt(6, obj.getCategoryid());
            ps.setInt(7, obj.getId());
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(null, ps, conn);
        }
        return 0;
    }
}
